package cn.easyar.samples.helloarqrcode.adpter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.easyar.samples.helloarqrcode.R;
import cn.easyar.samples.helloarqrcode.bean.MessageFileBean;
import cn.easyar.samples.helloarqrcode.remote.VideoChatViewActivity;

public class MessagesAdapter extends BaseQuickAdapter<MessageFileBean,MessagesAdapter.ViewHolder> {
    private VideoChatViewActivity context;
    private List<MessageFileBean> data;
    private boolean isFirst = true;

    public MessagesAdapter(@Nullable List<MessageFileBean> data, VideoChatViewActivity context) {
        super(R.layout.item_messages_layout, data);
        this.context = context;
        this.data = data;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    protected void convert(MessagesAdapter.ViewHolder helper, final MessageFileBean item) {
//        helper.itemView.setBackgroundColor(Color.TRANSPARENT);
        if(item.getFileType().equals("DOC")||item.getFileType().equals("DOCX")){
            helper.iv_item_message.setBackgroundResource(R.drawable.ic_doc);
        }else if(item.getFileType().equals("PDF")){
            helper.iv_item_message.setBackgroundResource(R.drawable.ic_pdf);
        }else if(item.getFileType().equals("MP4")){
            Glide.with( context )
                    .load(item.getUrl())
                    .thumbnail( 0.1f )
                    .into(helper.iv_item_message);
        }else if(item.getFileType().equals("TXT")){
            helper.iv_item_message.setBackgroundResource(R.drawable.ic_txt);
        }else if(item.getFileType().equals("JPG")||item.getFileType().equals("PNG")){
            Glide.with( context )
                    .load(item.getUrl())
                    .into(helper.iv_item_message);
        }else{
            Glide.with( context )
                    .load("")
                    .into(helper.iv_item_message);
        }

        helper.iv_item_message_name.setText(item.getFileName());
        helper.tv_item_message_user.setText(item.getUserName());
        helper.rl_item_message.setNextFocusRightId(helper.rl_item_message.getId());
        helper.rl_item_message.setNextFocusLeftId(R.id.btn_messages_close);
//        helper.rl_item_message.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if(hasFocus){
//                    v.setBackgroundColor(Color.BLUE);
//                }else{
//                    v.setBackgroundColor(Color.TRANSPARENT);
//                }
//            }
//        });

//        if(helper.getPosition()==0&&isFirst){
//            helper.rl_item_message.setNextFocusUpId(helper.rl_item_message.getId());
//        }

        if(helper.getPosition()==data.size()-1&&isFirst){
            helper.rl_item_message.requestFocus();
            isFirst = false;
        }



        //每次打开  焦点变成最后一个
        if(context.lastIndex != -1){
            helper.rl_item_message.requestFocus();
            context.lastIndex = -1;
        }
    }


    class ViewHolder extends BaseViewHolder {
        @BindView(R.id.rl_item_message)
        RelativeLayout rl_item_message;
        @BindView(R.id.iv_item_message)
        ImageView iv_item_message;
        @BindView(R.id.iv_item_message_name)
        TextView iv_item_message_name;
        @BindView(R.id.tv_item_message_user)
        TextView tv_item_message_user;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
